const WebSocketServer = require('websocket').server
const http = require('http')
const server = http.createServer()

wsServer = new WebSocketServer({
	httpServer: server
})

wsServer.on('request', (request) => {
	const connection = request.accept(null, request.origin)
	let robot1Data = {
		axis1: 0,
		axis2: 0,
		axis3: 0,
		axis4: 0,
		axis5: 0,
		axis6: 0
	}
	setInterval(() => {
		robot1Data.axis1 = (robot1Data.axis1 + Math.PI / 180) % (Math.PI * 2)
		robot1Data.axis4 = (robot1Data.axis4 + Math.PI / 30) % (Math.PI * 2)
		connection.sendUTF(JSON.stringify(robot1Data))
	}, 20)
	connection.on('close', (reasonCode, description) => {
		console.log('连接关闭')
	})
})

server.listen(3000, () => {
	console.log('服务器开启成功，请打开localhost:3000')
})
